package com.search.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.search.pojo.SaleResult;
import com.search.pojo.Spotssaleinfo;
import com.search.pojo.Statinfo;
import com.search.service.HttpMessage;
import com.search.utils.FirstEndOfMonth;

@RestController
public class SaleController {
  @Autowired
  private HttpMessage httpMessage;
  @RequestMapping("/getStatInfo")
  public List<Statinfo> getStatInfo(String beginTime,String endTime,String isOnline){
	  List<Statinfo>  list =httpMessage.getStatInfo(beginTime+" 00:00:00", endTime+" 00:00:00","tjDrCMdP5V");
	  if("1".equals(isOnline)){
		  list.remove(1);
	  }else {
		  list.remove(0); 
	  }
	  return list;
  }
  @RequestMapping("/getYouKeCenter")
  public List<SaleResult> getSpotsSaleInfo(String beginTime,String endTime){
	  
	  List<Spotssaleinfo> list =httpMessage.getSpotsSaleInfo(beginTime+" 00:00:00", endTime+" 00:00:00","tjDrCMdP5V","006");
	  Double ll=0.00;
	  Long tt=0l;
	  List<SaleResult> result =new ArrayList<>();
	  for(Spotssaleinfo info :list){
		  ll+= info.getPaysum();
		  tt=tt+Long.parseLong(info.getTicketnocount());
	  }
	  SaleResult sale=new SaleResult();
	  sale.setParkName(list.get(0).getParkfullname());
	  sale.setPaySum(ll);
	  sale.setTicketSum(tt);
	  result.add(sale);
	  return result;
  }
  /**
   * 按月查询游客中心的购票信息
   * @return
   */
  @RequestMapping("/getYouKeCenterMonth")
  public List<SaleResult> getSpotsSaleInfoMonth(){
	  Calendar cal=Calendar.getInstance();
	  String beginTime=FirstEndOfMonth.getFirstDayofMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
	  String endTime=FirstEndOfMonth.getEndDayofMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
	  List<SaleResult> list =getSpotsSaleInfoCommon(beginTime,endTime);
	  return list;
  }
  /**
   * 按周查询
   * @return
   */
  @RequestMapping("/getYouKeCenterWeek")
  public List<SaleResult> getSpotsSaleInfoWeek(){
	  String beginTime=FirstEndOfMonth.getFirstWeekDay();
	  String endTime=FirstEndOfMonth.getLastWeekDay();
	  List<SaleResult> list =getSpotsSaleInfoCommon(beginTime,endTime);
	  return list;
  }
  /**
   * 按日查询
   * @return
   */
  @RequestMapping("/getYouKeCenterDay")
  public List<SaleResult> getSpotsSaleInfoDay(){
	  Calendar cal = Calendar.getInstance();
	  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	  String beginTime=df.format(cal.getTime());
	  cal.add(Calendar.DAY_OF_YEAR, 1);
	  String endTime=df.format(cal.getTime());
	  List<SaleResult> list =getSpotsSaleInfoCommon(beginTime,endTime);
	  return list;
  }
  /**
   * 
   * @param beginTime
   * @param endTime
   * @return
   */
  @RequestMapping("/getLongTanWu")
  public List<SaleResult> getLongTanWu(String beginTime,String endTime){
	  List<Spotssaleinfo> list1 =httpMessage.getSpotsSaleInfo(beginTime+" 00:00:00", endTime+" 00:00:00","tjDrCMdP5V","004");
	  List<Spotssaleinfo> list2 =httpMessage.getSpotsSaleInfo(beginTime+" 00:00:00", endTime+" 00:00:00","tjDrCMdP5V","005");
	  Double ll=0.00;
	  Long tt=0l;
	  List<SaleResult> result =new ArrayList<>();
	  for(Spotssaleinfo info :list1){
		  ll+= info.getPaysum();
		  tt=tt+Long.parseLong(info.getTicketnocount());
	  }
	  for(Spotssaleinfo info :list2){
		  ll+= info.getPaysum();
		  tt=tt+Long.parseLong(info.getTicketnocount());
	  }
	  SaleResult sale=new SaleResult();
	  sale.setParkName(list1.get(0).getParkfullname()+"和"+list2.get(0).getParkfullname());
	  sale.setPaySum(ll);
	  sale.setTicketSum(tt);
	  result.add(sale);
	  return result;
  }
  /**
   * 按月查询
   * @return
   */
  @RequestMapping("/getLongTanWuMonth")
  public List<SaleResult> getLongTanWuMonth(){
	  Calendar cal=Calendar.getInstance();
	  String beginTime=FirstEndOfMonth.getFirstDayofMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
	  String endTime=FirstEndOfMonth.getEndDayofMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
	  List<SaleResult> list =getLongTanWuCommon(beginTime,endTime);
	  return list;
  }
  /**
   * 按周查询
   * @return
   */
  @RequestMapping("/getLongTanWuWeek")
  public List<SaleResult> getLongTanWuWeek(){
	  String beginTime=FirstEndOfMonth.getFirstWeekDay();
	  String endTime=FirstEndOfMonth.getLastWeekDay();
	  List<SaleResult> list =getLongTanWuCommon(beginTime,endTime);
	  return list;
  }
  /**
   * 按日查询
   * @return
   */
  @RequestMapping("/getLongTanWuDay")
  public List<SaleResult> getLongTanWuDay(){
	  Calendar cal = Calendar.getInstance();
	  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	  String beginTime=df.format(cal.getTime());
	  cal.add(Calendar.DAY_OF_YEAR, 1);
	  String endTime=df.format(cal.getTime());
	  List<SaleResult> list =getLongTanWuCommon(beginTime,endTime);
	  return list;
  }
  /**
   * 查询每月的检票和售票数量
   * @param isOnline
   * @return
   */
  @RequestMapping("/getStatInfoMonth")
  public List<Statinfo> getStatInfoMonth(String isOnline){
	  Calendar cal=Calendar.getInstance();
	  String beginTime=FirstEndOfMonth.getFirstDayofMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
	  String endTime=FirstEndOfMonth.getEndDayofMonth(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
	  List<Statinfo> list =this.getStatInfoCommon(beginTime, endTime, isOnline);
	  return list;
  }
  /**
   * 查询每周的检票和售票数量
   * @param isOnline
   * @return
   */
  @RequestMapping("/getStatInfoWeek")
  public List<Statinfo> getStatInfoWeek(String isOnline){
	 
	  String beginTime=FirstEndOfMonth.getFirstWeekDay();
	  String endTime=FirstEndOfMonth.getLastWeekDay();
	  List<Statinfo> list =this.getStatInfoCommon(beginTime, endTime, isOnline);
	  return list;
  }
  @RequestMapping("/getStatInfoDay")
  public List<Statinfo> getStatInfoDay(String isOnline){
	  Calendar cal = Calendar.getInstance();
	  SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	  String beginTime=df.format(cal.getTime());
	  cal.add(Calendar.DAY_OF_YEAR, 1);
	  String endTime=df.format(cal.getTime());
	  List<Statinfo> list =this.getStatInfoCommon(beginTime, endTime, isOnline);
	  return list;
  }
  /**
   * 
   * @param beginTime
   * @param endTime
   * @param isOnline
   * @return
   */
  private List<Statinfo> getStatInfoCommon(String beginTime,String endTime,String isOnline){
	  List<Statinfo>  list =httpMessage.getStatInfo(beginTime+" 00:00:00", endTime+" 00:00:00","tjDrCMdP5V");
	  if("1".equals(isOnline)){
		  list.remove(1);
	  }else {
		  list.remove(0); 
	  }
	  return list;
  }
  /**
   * 
   * @param beginTime
   * @param endTime
   * @return
   */
  private List<SaleResult> getSpotsSaleInfoCommon(String beginTime,String endTime){
	  List<Spotssaleinfo> list =httpMessage.getSpotsSaleInfo(beginTime+" 00:00:00", endTime+" 00:00:00","tjDrCMdP5V","006");
	  Double ll=0.00;
	  Long tt=0l;
	  List<SaleResult> result =new ArrayList<>();
	  for(Spotssaleinfo info :list){
		  ll+= info.getPaysum();
		  tt=tt+Long.parseLong(info.getTicketnocount());
	  }
	  SaleResult sale=new SaleResult();
	  sale.setParkName(list.get(0).getParkfullname());
	  sale.setPaySum(ll);
	  sale.setTicketSum(tt);
	  result.add(sale);
	  return result;
  }
  /**
   * 
   * @return
   */
  private List<SaleResult> getLongTanWuCommon(String beginTime,String endTime){
	  List<Spotssaleinfo> list1 =httpMessage.getSpotsSaleInfo(beginTime+" 00:00:00", endTime+" 00:00:00","tjDrCMdP5V","004");
	  List<Spotssaleinfo> list2 =httpMessage.getSpotsSaleInfo(beginTime+" 00:00:00", endTime+" 00:00:00","tjDrCMdP5V","005");
	  Double ll=0.00;
	  Long tt=0l;
	  List<SaleResult> result =new ArrayList<>();
	  for(Spotssaleinfo info :list1){
		  ll+= info.getPaysum();
		  tt=tt+Long.parseLong(info.getTicketnocount());
	  }
	  for(Spotssaleinfo info :list2){
		  ll+= info.getPaysum();
		  tt=tt+Long.parseLong(info.getTicketnocount());
	  }
	  SaleResult sale=new SaleResult();
	  sale.setParkName(list1.get(0).getParkfullname()+"和"+list2.get(0).getParkfullname());
	  sale.setPaySum(ll);
	  sale.setTicketSum(tt);
	  result.add(sale);
	  return result; 
  }
}
